from flask import render_template
nav={
    'index':['首页','index.html',{}],
    'spider':['爬虫','spider.html',{}],
    'panda':['数据','panda.html',{}],
    'fax':['传真','fax.html',{
        'config':['基础资料设置','fax_config.html']
        ,'receiveBox':['收件箱','fax_receiveBox.html'] 
        ,'sendBox':['发件箱','fax_sendBox.html'] 
        ,'fax':['发送传真','fax.html'] 
    }],
}

#初始模板数据字典
def fdata(key,key2):
    Fdata={'nav':nav}
    Fdata['title']=Fdata['nav'][key][0]
    Fdata['nav2']=Fdata['nav'][key][2]
    if key2 =='':
        Fdata['title2']=''
        Fdata['html']=Fdata['nav'][key][1]
        Fdata['titleAll']=Fdata['title']
    else:
        Fdata['title2']=Fdata['nav'][key][2][key2][0]
        Fdata['html']=Fdata['nav'][key][2][key2][1]
        Fdata['titleAll']=f'''{Fdata['title']} {Fdata['title2']}'''
    return Fdata

def normalView(**kwargs):
    Fdata=fdata(kwargs['key'],kwargs['key2'])
    return  render_template(Fdata['html'],Fdata=Fdata)

#根据nav批量添加路由
def forAdd(app):
    for k in nav:
        if nav[k][2]:
            for k2 in nav[k][2]:
                app.add_url_rule('/'+nav[k][2][k2][1],view_func=normalView,defaults={'key':k,'key2':k2})
        else:
            app.add_url_rule('/'+nav[k][1],view_func=normalView,defaults={'key':k,'key2':''})
    return app
